package com.example.wangandroid.adapter;

import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.FileProvider;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.example.wangandroid.BuildConfig;
import com.example.wangandroid.R;
import com.kongzue.dialogx.dialogs.PopMenu;
import com.kongzue.dialogx.interfaces.OnMenuItemClickListener;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

import ui.XKSlideLayout;

public class VideoMp3Adapter extends RecyclerView.Adapter<VideoMp3Adapter.VH> {

    View.OnClickListener click;
    private final Context mContext;
    private List<File> fdata;

    private MediaPlayer mediaPlayer;
    public VideoMp3Adapter(Context context, List<File> data) {
        this.mContext = context;
        fdata = data;
        mediaPlayer = new MediaPlayer();
    }

    public void setdata(List<File> data) {
        fdata = data;
    }

    static class VH extends RecyclerView.ViewHolder{

        TextView tv_name;
        ImageView imageView;

        LinearLayout linearLayout;


        public VH(@NonNull View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_filename);
            imageView = itemView.findViewById(R.id.iv_play);
            linearLayout = itemView.findViewById(R.id.ll);
        }
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.activity_videolist, parent, false);
        return new VH(view);
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, int position) {
        holder.tv_name.setText(fdata.get(position).getName());
        LogUtils.d(fdata.get(holder.getBindingAdapterPosition()).getPath());
        holder.linearLayout.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                LogUtils.d("onlongclick");
                PopMenu.show(new String[]{"删除"})
                        .setOnMenuItemClickListener(new OnMenuItemClickListener<PopMenu>() {
                            @Override
                            public boolean onClick(PopMenu dialog, CharSequence text, int index) {
                                File f = new File(fdata.get(position).getPath());
                                if(index == 0){
                                    f.delete();
                                    File localf = new File(mContext.getExternalFilesDir(null).toURI());
                                    List<File> data = List.of(Objects.requireNonNull(localf.listFiles()));
                                     setdata(data);
                                    notifyDataSetChanged();
                                }else if(index == 1){

                                }
                                return false;
                            }
                        });

                return false;
            }
        });
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    mediaPlayer.reset();
                    mediaPlayer.setDataSource(fdata.get(position).getPath());
                    LogUtils.d(fdata.get(holder.getBindingAdapterPosition()).getPath());
                    mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mediaPlayer) {
                            mediaPlayer.start();
                        }
                    });
                    mediaPlayer.prepareAsync();
                    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mediaPlayer) {
                            mediaPlayer.stop();
                        }
                    });
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }finally {

                }

            }
        });
    }


    @Override
    public int getItemCount() {
        if(fdata==null||fdata.size()==0){
            return 0;
        }
        return fdata.size();
    }


    @Override
    public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        if(mediaPlayer!=null){
            mediaPlayer.release();
        }
        mediaPlayer=null;
    }
}
